import json

from collections import OrderedDict

code_dict = OrderedDict()

with open('anchor_knowledge.txt','r',encoding='utf-8') as fr:
    lines = fr.readlines()
    for line in lines:
        knowledges = json.loads(line.strip().split("\t")[-1])
        for knowledge in knowledges:
            code = knowledge.get('code')
            name = knowledge.get('name')
            if code not in code_dict.keys():
                code_dict[code] = name
code_list = []               
for key,value in code_dict.items():
    line = key + '\t' + value + '\n'
    code_list.append(line)
with open('anchor_knowledge_map.txt','w',encoding='utf-8') as fw:
    fw.writelines(code_list)
print('文件写入完成')           
        


